﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Project.Module.DAO.IBL;
using Project.Module.Model;
using Project.Engine.Utilities;

namespace Project.Module.DAO.BL
{
    public class GioHangBL : IBLTable<CartModel, int>
    {
        public List<GioHangModel> GetGioHangByUsername(string name)
        {
            int customerId = (new CustomerBL()).GetIdByUsername(name);

            var queryCart = from e in this.Context.Carts
                            join a in this.Context.Products
                            on e.ProductID equals a.ProductID
                            where e.CustomerID == customerId && (
                                (e.IsCheckOut == null || (e.IsCheckOut != null && e.IsCheckOut == false)) &&
                                (e.IsDelivery == null ||  (e.IsDelivery != null && e.IsDelivery == false))
                            ) 
                            select new GioHangModel()
                            {
                                idCart = e.CartID,
                                idProduct = e.ProductID,
                                CustomerId = e.CustomerID,
                                nameProduct = a.ProductName,
                                priceProduct = a.Price,
                                quantityProduct = e.Quantity != null ? e.Quantity.Value : 0,
                                money = e.Quantity != null ? a.Price * e.Quantity.Value : 0,
                                status = e.IsDelivery == true ? "Đã thanh toán" : "Chưa thanh toán",
                                CreatedDate = e.CreatedDate.HasValue ? e.CreatedDate.Value : Convert.ToDateTime("01/01/1900"),
                            };

            if (queryCart != null && queryCart.ToList().Count > 0) {
                return queryCart.ToList();
            } else {
                return null;
            }
        }

        public List<GioHangModel> GetGioHang()
        {
            var queryCart = from e in this.Context.Carts
                            join a in this.Context.Products
                            on e.ProductID equals a.ProductID
                            join cus in this.Context.Customers
                            on e.CustomerID equals cus.CustomerID
                            where e.IsDelivery == true
                            orderby e.CreatedDate descending
                            select new GioHangModel()
                            {
                                idCart = e.CartID,
                                idProduct = e.ProductID,
                                CustomerId = e.CustomerID,
                                nameProduct = a.ProductName,
                                priceProduct = a.Price,
                                quantityProduct = e.Quantity != null ? e.Quantity.Value : 0,
                                money = e.Quantity != null ? a.Price * e.Quantity.Value : 0,
                                status = e.IsDelivery == true ? "Đã thanh toán" : "Chưa thanh toán",                                                         CustomerName = cus.Username,
                                CustomerMail = cus.Email,
                                CustomerAddress = cus.Address,
                                CustomerPhone = cus.Phonenumber,
                                CreatedDate = e.CreatedDate.HasValue ? e.CreatedDate.Value : Convert.ToDateTime("01/01/1900"),
                            };

            if (queryCart != null && queryCart.ToList().Count > 0)
            {
                return queryCart.ToList();
            }
            else
            {
                return null;
            }
        }
    }
}
